package com.bookstore.bauhinia.category.controller;

import com.bookstore.bauhinia.category.service.CategoryService;
import com.bookstore.bauhinia.pojo.Category;
import com.bookstore.bauhinia.pojo.CommonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @PostMapping("/category/insert")
    public CommonResult insertCategory(@RequestBody Category record){
        int result = categoryService.insert(record);
        System.out.println("*****插入结果:" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功",record);
        } else {
            return new CommonResult(444,"插入数据库失败",null);
        }
    }
    @GetMapping("/category/get/{id}")
    public CommonResult getCategoryById(@PathVariable("id") int id){
        Category category = categoryService.selectByPrimaryKey(id);
        System.out.println("*****查询结果:" + category);
        if (category != null) {
            return new CommonResult(200, "查询成功",category);
        } else {
            return new CommonResult(444,"没有对应记录,查询id:" + id,null);
        }
    }
    @GetMapping("/category/delete/{id}")
    public CommonResult deleteCategoryById(@PathVariable("id") int id){
        int result = categoryService.deleteByPrimaryKey(id);
        if (result > 0) {
            return new CommonResult(200, "删除数据成功");
        } else {
            return new CommonResult(444,"删除数据失败");
        }
    }
    @PostMapping("/category/update")
    public CommonResult updateCategory(@RequestBody Category record){
        int result = categoryService.updateByPrimaryKey(record);
        if (result > 0) {
            return new CommonResult(200, "更新数据成功",record);
        } else {
            return new CommonResult(444,"更新数据失败",null);
        }
    }

    @GetMapping("/category/all")
    public CommonResult getAllCategories(){
        List<Category> categories = categoryService.getAllCategories();
        if(categories != null){
            return new CommonResult(200, "查询数据成功", categories);
        } else{
            return new CommonResult(444, "查询数据失败", null);
        }
    }
}
